/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo1;

import Acceso.Security;
import Modelo.DAL.Novedad_DAO;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
/**
 *
 * @author Hector Gutierrez
 */
public class consultarNovedadAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI"};
        if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }

        String id_gen = request.getParameter("id_gen");
        String id_esp = request.getParameter("id_esp");
        if (id_gen == null || id_gen.equals("")) {
            return mapping.findForward(FAILURE);
        }
        System.out.println("Se recibieron los parámetros: "+id_gen+" y "+id_esp);
        response.setContentType("text/json");
        JSONObject jo = new JSONObject();
        if("-1".equals(id_esp)){
            System.out.println("GENERICA");
            jo.put("novedad", Novedad_DAO.obtenerNovedadGenerica(Integer.parseInt(id_gen)));
        }else{
            System.out.println("ESPECIFICA");
            jo.put("novedad", Novedad_DAO.obtenerNovedadEspecifica(Integer.parseInt(id_gen),Integer.parseInt(id_esp)));
        }
        PrintWriter pw = response.getWriter();
        pw.write(jo.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
